package com.yzdmw.toutiao.user;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yzdmw.toutiao.R;
import com.yzdmw.toutiao.history.HistoryActivity;
import com.yzdmw.toutiao.login.LoginActivity;

public class UserFragment extends Fragment {

    private UserViewModel mViewModel;
    private ListView settings;
    private Button articleBtn;
    private Button changeBtn;
    private Button exitBtn;
    private ImageButton historyButton;
    private ImageButton likeButton;
    private ImageButton commentButton;
    private ImageButton messageButton;

    public static UserFragment newInstance() {
        return new UserFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.user_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(requireActivity()).get(UserViewModel.class);
        mViewModel.getIsSigned().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable final Boolean isSigned) {
                // Update the UI, in this case, a TextView.
                if(isSigned) {
                    getActivity().getSupportFragmentManager().beginTransaction()
                            .remove(UserUnsignedFragment.newInstance())
                            .replace(R.id.fcv, UserSignedFragment.newInstance())
                            .commit();
                }
                else {
                    getActivity().getSupportFragmentManager().beginTransaction()
                            .remove(UserSignedFragment.newInstance())
                            .replace(R.id.fcv, UserUnsignedFragment.newInstance())
                            .commit();
                }
            }
        });
        settings = getView().findViewById(R.id.settings);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.setting_item, mViewModel.getSettings());
        settings.setAdapter(adapter);
        settings.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getContext(), adapter.getItem(position), Toast.LENGTH_SHORT).show();
                //TODO:各个设置项
            }
        });
        articleBtn = getView().findViewById(R.id.user_article_btn);
        articleBtn.setOnClickListener(view -> {
            if(mViewModel.getIsSigned().getValue()) {
                Toast.makeText(getContext(), "文章发布", Toast.LENGTH_SHORT).show();
                //TODO:文章发布功能
            }
            else {
                Toast.makeText(getContext(), "请先登录账户", Toast.LENGTH_SHORT).show();
            }
        });
        changeBtn = getView().findViewById(R.id.user_change_btn);
        changeBtn.setOnClickListener(view -> {
            if(mViewModel.getIsSigned().getValue()) {
                navigateTo("切换登陆");
            }
            else {
                Toast.makeText(getContext(), "请先登录账户", Toast.LENGTH_SHORT).show();
            }
        });
        exitBtn = getView().findViewById(R.id.user_exit_btn);
        exitBtn.setOnClickListener(view -> {
            if(mViewModel.getIsSigned().getValue()) {
                logout();
            }
            else {
                Toast.makeText(getContext(), "请先登录账户", Toast.LENGTH_SHORT).show();
            }
        });
        historyButton = getView().findViewById(R.id.user_historyButton);
        historyButton.setOnClickListener(view -> {
            navigateTo("历史");
        });
        likeButton = getView().findViewById(R.id.user_likeButton);
        likeButton.setOnClickListener(view -> {
            navigateTo("点赞");
        });
        commentButton = getView().findViewById(R.id.user_commentButton);
        commentButton.setOnClickListener(view -> {
            navigateTo("评论");
        });
        messageButton = getView().findViewById(R.id.user_messageButton);
        messageButton.setOnClickListener(view -> {
            Toast.makeText(getContext(), "消息通知", Toast.LENGTH_SHORT).show();
            //TODO:消息功能
        });
    }


    private void logout() {
        mViewModel.setIsSigned(false);
        // TODO: 调用网络接口的logout
    }

    private void navigateTo(String destination) {
        Intent intend;
        switch (destination) {
            case "切换登陆":
                intend = new Intent(requireActivity(), LoginActivity.class);
                startActivityForResult(intend,10); // TODO: 切换用户需要进行登陆和获取新用户消息，失败则不处理
                break;
            default:
                intend = new Intent(requireActivity(), HistoryActivity.class);
                intend.putExtra("tab",destination);
                startActivity(intend);
                break;
        }
    }

    private void navigateToHistory() {
        Intent intent = new Intent(requireActivity(), HistoryActivity.class);
        requireActivity().startActivity(intent);
    }

}